/**
 * Copyright (c) 2012 Nokia Corporation. 
 */

package com.nokia.example.tictactoe.ui;

import javax.microedition.lcdui.Graphics;

import com.nokia.mid.ui.CanvasGraphicsItem;

/**
 * A custom progress bar.
 */
public class ProgressBar extends CanvasGraphicsItem {
	// Constants
	public static final int PROGRESS_BAR_HEIGHT = 4;
	public static final int DEFAULT_PROGRESS_BAR_COLOR = 0xff5577ee;
	public static final int MAX_VALUE = 100;
	
	// Members
	protected final int _width;
	protected int _value = 0;
	protected int _x = 0;
	protected int _color = DEFAULT_PROGRESS_BAR_COLOR;

	/**
	 * Constructor.
	 * @param width The maximum width of the progress bar.
	 */
	public ProgressBar(int width) {
		super(width, PROGRESS_BAR_HEIGHT);
		_width = width;
	}
	
	/**
	 * From CanvasGraphicsItem.
	 */
	protected void paint(Graphics graphics) {
		graphics.setColor(_color);
		graphics.fillRect(0, 0, _x, PROGRESS_BAR_HEIGHT);
	}

	/** 
	 * @param value The value to set [0, MAX_VALUE].
	 */
	public void setValue(int value) {
		if (value >= 0 && value <= MAX_VALUE) {
			_value = value;
			
			if (_value == 0) {
				_x = 0;
			}
			else {
				_x = _width * _value / MAX_VALUE;
			}
		}
		
		repaint();
	}
	
	/** 
	 * @param color The color to set for the progress bar.
	 */
	public void setColor(int color) {
		_color = color;
		repaint();
	}
}
